package com.smartian.v2.utils;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;

import com.smartian.v2.htmltext.HtmlTagHandler;
import com.smartian.v2.htmltext.SpanTag;
import com.smartian.v2.model.DateTimeNode;
import com.smartian.v2.model.ScheduleTaskBean;

import java.util.List;

/**
 * Created by tianweitong on 2020/2/26.
 */

public class TextRenderHandler {

    private static TextRenderHandler instance;
    private final HtmlTagHandler htmlTagHandler = new HtmlTagHandler();

    public static TextRenderHandler getInstance() {
        if (instance == null) {
            instance = new TextRenderHandler();
        }
        return instance;
    }

    public CharSequence getResultContent(Context context, ScheduleTaskBean scheduleTaskInfo) {

        if (scheduleTaskInfo == null) return null;
        List<DateTimeNode> dateTimeNodes = scheduleTaskInfo.getDateTimeNodes();
        if (dateTimeNodes == null || dateTimeNodes.size() == 0) {
            return scheduleTaskInfo.getSourceText();
        }
        String sourceText = scheduleTaskInfo.getSourceText();
        if (TextUtils.isEmpty(sourceText)) {
            return sourceText;
        }
        String repeatText = scheduleTaskInfo.getRepeatText();
        for (int i = 0; i < dateTimeNodes.size(); i++) {
            DateTimeNode dateTimeNode = dateTimeNodes.get(i);
            String text = dateTimeNode.text;

            if (!TextUtils.isEmpty(text)) {

                if (!TextUtils.isEmpty(repeatText)) {
                    if (repeatText.contains(text)) {
                        text = repeatText;
                    } else if (sourceText.contains(repeatText) && sourceText.contains(text)) {
                        int repeatLastTextIndex = sourceText.indexOf(repeatText) + repeatText.length() - 1;
                        int textIndex = sourceText.indexOf(text);
                        if (textIndex != -1 && textIndex == repeatLastTextIndex) {
                            text = repeatText.substring(0, repeatText.length() - 1).concat(text);
                        }
                        int repeatTextIndex = sourceText.indexOf(repeatText);
                        int textLastIndex = sourceText.indexOf(text) + text.length() - 1;
                        if (textLastIndex != -1 && textLastIndex == repeatTextIndex) {
                            text = text.substring(0, text.length() - 1).concat(repeatText);
                        }
                    }
                }
                sourceText = sourceText.replaceAll(text, "<span style='text-color:#FFFF8C14;'>" + text + "</span>");
            }
            String locatorText = dateTimeNode.locatorText;

            if (!TextUtils.isEmpty(locatorText)) {
                sourceText = sourceText.replaceAll(locatorText, "<span style='text-color:#FFFF8C14;'>" + locatorText + "</span>");
            }
        }

        if (!TextUtils.isEmpty(repeatText)) {
            sourceText = sourceText.replaceAll(repeatText, "<span style='text-color:#FFFF8C14;'>" + repeatText + "</span>");
        }
        Spanned spanned = TextRenderHandler.getInstance().renderHtmlText(context, sourceText);
        return spanned;
    }

    private Spanned renderHtmlText(Context context, String text) {

        htmlTagHandler.registerTag("span", new SpanTag(context));
        String html = String.format("<html>%s</html>", text);
        final Spanned spanned = Html.fromHtml(html, htmlTagHandler, htmlTagHandler);
        htmlTagHandler.unregisterTag("span");
        return spanned;
    }

}
